package com.example.zhou.socketdemo;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.SocketHandler;

public class MainActivity extends AppCompatActivity {
    Button conBtn;
    EditText ipText;
    Button sendBtn;
    EditText sendText;
    TextView textView;
    Socket clientSocket;
    BufferedReader bfReader;
    BufferedWriter bfWriter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        conBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                connect();
            }
        });
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send();
            }
        });
    }

    private void findViews() {
        conBtn = (Button) findViewById(R.id.con_btn);
        sendBtn = (Button) findViewById(R.id.send_btn);
        ipText = (EditText) findViewById(R.id.ip_text);
        sendText = (EditText) findViewById(R.id.send_text);
        textView = (TextView) findViewById(R.id.content_text);
    }

    private void connect() {
        AsyncTask<Void, String, Void> reader = new AsyncTask<Void, String, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    clientSocket = new Socket("192.168.191.1", 13345);
                    //客户端建立与服务端socket的连接,"10.62.37.152"为我的局域网ip地址,读者按照自己的ip地址进行相应修改
                    bfWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
                    bfReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    String line = "hello";
                    while ((line = bfReader.readLine()) != null) {
                        publishProgress(line);//接收从服务端转发来的消息
                    }
                } catch (IOException e) {
                    Toast.makeText(getApplicationContext(), "读取消息失败", Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onProgressUpdate(String... values) {
                textView.append("别人说:" + values[0] + "\n");
                super.onProgressUpdate(values);
            }
        };
        reader.execute();
    }

    //发送消息给服务端
    private void send() {
        try {
            textView.append("我说" + sendText.getText().toString() + "\n");
            bfWriter.write(sendText.getText().toString() + "\n");
            bfWriter.flush();
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), "无法建立连接", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
}